<template>
  <div>
  <el-table
      v-loading="loading"
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange">
    <el-table-column
        type="selection"
        width="55">
    </el-table-column>
    <el-table-column
        prop="id"
        label="购物车id"
        width="120">
    </el-table-column>
    <el-table-column
        prop="productName"
        label="产品名称"
        width="120">
    </el-table-column>
    <el-table-column
        prop="quantity"
        label="数量"
        show-overflow-tooltip>
    </el-table-column>
    <el-table-column
        prop="price"
        label="单价"
        show-overflow-tooltip>
    </el-table-column>
    <el-table-column
        fixed="right"
        label="操作"
        width="100">
      <template slot-scope="scope">
        <el-button @click="del(scope.row)" type="text" size="small">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
    <div style="margin-right: 10px;margin-top: 20px;margin-right: 40px">
        <el-col :span="12">
          <div class="left">
            <span>总金额为{{this.totalPrice}}元</span>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="right" style="text-align: right">
            <el-button type="success" plain @click="addOrder">下单</el-button>
          </div>
        </el-col>
    </div>
    <el-dialog title="选择收获地址和支付方式" :visible.sync="dialogTableVisible">
      <h4 style="margin-bottom: 20px">订单总金额为{{this.totalPrice2}}元</h4>
      <el-form ref="form" :model="form">
        <el-form-item label="收获地址" prop="addrId">
          <el-select v-model="form.addrId" placeholder="请选择收获地址">
            <el-option v-for="item in optionItem" :label="item.addr" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付方式" prop="payType">
          <el-select v-model="form.type" placeholder="请选择支付方式">
            <el-option label="在线支付" value="1"></el-option>
            <el-option label="货到付款" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="margin-top: 20px">
          <el-button type="primary" @click="onSubmit('form')">立即下单</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<!--style="margin-right: 30px"-->
<script>
export default {
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },
    del(row){
      const _this=this;
      this.$confirm('删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.post('http://localhost:8181/cart/delById/'+row.id).then(function(resp){
          if(resp.data==0){
            _this.$message.error('删除失败!');
          }else{
            _this.$message({
              type: 'success',
              message: '删除成功!'
            });
            window.location.reload();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    addOrder(){
      this.dialogTableVisible = true;
      const _this=this;
      axios.get('http://localhost:8181/user/findAddressByAccount/'+this.$store.state.account).then(function (resp){
        _this.optionItem=resp.data.data;
      })
      this.getTotalPrice2();
    },
    onSubmit(formName){
      this.form.productList=this.multipleSelection;
      const _this=this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.form)
          axios.post('http://localhost:8181/user/order',this.form).then(function (resp){
            if(resp.data=='1'){
              _this.$message({
                message: '下单成功',
                type: 'success'
              });
              window.location.reload();
            }else{
              _this.$message({
                message: '下单失败',
                type: 'warning'
              });
            }
          })
        }
      });
    },
    getTotalPrice(){
      let a=0;
      for(let i=0; i<this.total;i++){
        a=a+this.tableData[i].price*this.tableData[i].quantity;
      }
      this.totalPrice=a;
    },
    getTotalPrice2(){
      let a=0;
      for(let i=0; i<this.multipleSelection.length;i++){
        a=a+this.multipleSelection[i].price*this.multipleSelection[i].quantity;
      }
      this.totalPrice2=a;
    }
  },
  created() {
    const _this=this;
    axios.get('http://localhost:8181/cart/findAllByAccount/'+this.$store.state.account).then(function(resp){
      _this.tableData=resp.data.data
      _this.total=resp.data.totalNum
      _this.loading=false
    });
    setTimeout(() => {
      this.getTotalPrice()
    }, 500)
  },
  data() {
    return {
      totalPrice:null,
      totalPrice2:null,
      total:null,
      tableData:null,
      loading: true,
      multipleSelection: [],
      dialogTableVisible: false,
      optionItem:[],
      form:{
        addrId:null,
        type:null,
        productList:null
      }
    }
  }
}
</script>

<style scoped>

</style>